<?php

namespace Admin\Controller;

use lyf\Str;
use Think\Controller;

require_once './vendor/autoload.php';


class BaseController extends Controller
{


    public function _initialize()
    {

        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }

         if(CONTROLLER_NAME!='Login'){
         	if(empty($_SESSION['adminster'])){
         		$this->redirect('Login/index');
                       exit;
         	}
         }

        //权限处理
        $admin = $_SESSION['admin'];
        if ($admin['role_id'] != 0) {
            $access = D('access')->where(array('role_id' => $admin['role_id']))->find();
            $accessid = explode("#", $access['nav_id']);
            $navaction = strtolower(CONTROLLER_NAME . '/' . ACTION_NAME);
            $i = 1;
            foreach ($accessid as $key => $value) {
                $navlist[$i] = D('nav')->find($value);
                $i++;
            }
            $navid = 0;
            foreach ($navlist as $k => $v) {
                if (strtolower($v['controller'] . '/' . $v['function']) == $navaction) {
                    $navid = (int)$k;
                    break;
                }
            }

            if (empty($navid)) {
                $controller_name = strtolower(CONTROLLER_NAME);
                $action_name = strtolower(ACTION_NAME);
                $nav_name = D('nav')->where("controller = '{$controller_name}' and function = '{$action_name}'")->getField('nav_name');
                $this->error('很抱歉您没有权限操作模块:' . $nav_name, U('Admin/index/index'));
            }
            $admininfo = M('agent')->where("id = {$admin['id']}")->find();
            session('admin', $admininfo);
        } else {

            $navlist = D('nav')->select();
        }

        $this->assign(array(
            'date_year' => date('Y'),
            'official_website_name' => 'BaYue',
            'official_website_url' => 'http://www.bayuenet.com',
            'navlist' => $navlist
        ));

    }


    function file_upload($type,$width=500)
    {
        if (empty($_FILES)) {
            return showErrorMsg('没有文件上传',100002);
        } else {

            $upload = new \Think\Upload();// 实例化上传类

            switch ($type){
                case 'video':
                    $upload->maxSize = 524288000;// 设置附件上传大小
                    $upload->exts = array('mp4','flv');// 设置附件上传类型
                    $upload->rootPath = './Uploads/'.$type.'/'; // 设置附件上传根目录
                    $upload->savePath = ''; // 设置附件上传（子）目录
                    $upload->saveRule = 'time';
                    $upload->hash = true;
                    $upload_ret = $upload->uploadOne($_FILES['file']);
                    if ($upload_ret == false) return showErrorMsg($upload->getError(),100002);

                    $imgUrl = empty($upload_ret) ? '' : '/Uploads/' .$type.'/'. $upload_ret['savepath'] . $upload_ret['savename'];

                    $insert_data = array(
                        'uid' => '',
                        'name' => $_FILES['file']['name'],
                        'path' => $imgUrl,
                        'ext' => $_FILES['file']['type'],
                        'size' => $_FILES['file']['size'],
                        'md5' => $upload_ret['md5'],
                        'sha1' => $upload_ret['sha1'],
                        'location' => 'location',
                        'download' => '0',
                        'base64' => '',
                        'url' => C('site_url') . $imgUrl,
                        'thumb' => '/Uploads/photo/' . $upload_ret['savepath'] . '500-' . $upload_ret['savename'],
                        'create_time' => NOW_TIME,
                        'update_time' => NOW_TIME,
                    );

                    $operation = M('file')->add($insert_data);

                    $upload_ret['url'] = $imgUrl;
                    $upload_ret['domain_url'] = C('site_url').$imgUrl;

                    break;
                case 'photo':
                    $upload->maxSize = 524288000;// 设置附件上传大小
                    $upload->exts = array('jpg','gif','jpeg','png');// 设置附件上传类型
                    $upload->rootPath = './Uploads/'.$type.'/'; // 设置附件上传根目录
                    $upload->savePath = ''; // 设置附件上传（子）目录
                    $upload->saveRule = 'time';
                    $upload->hash = true;
                    $upload_ret = $upload->uploadOne($_FILES['file']);
                    if ($upload_ret == false) {
                        $data = array(
                            'code' => 0,
                            'msg' => '上传失败',
                            'data' => $upload->getError()
                        );
                        echo  json_encode($data);
                        exit();
                    }

                    $imgUrl = empty($upload_ret) ? '' : '/Uploads/' .$type.'/'. $upload_ret['savepath'] . $upload_ret['savename'];


                    thumb($imgUrl, $width, '/Uploads/'.$type.'/' . $upload_ret['savepath'] . $width.'-' . $upload_ret['savename']);

                    $insert_data = array(
                        'uid' => '',
                        'name' => $_FILES['file']['name'],
                        'path' => $imgUrl,
                        'ext' => $_FILES['file']['type'],
                        'size' => $_FILES['file']['size'],
                        'md5' => $upload_ret['md5'],
                        'sha1' => $upload_ret['sha1'],
                        'base64' => '',
                        'location' => 'location',
                        'download' => '0',
                        'url' => C('site_url') . $imgUrl,
                        'thumb' => '/Uploads/photo/' . $upload_ret['savepath'] . '500-' . $upload_ret['savename'],
                        'create_time' => NOW_TIME,
                        'update_time' => NOW_TIME,
                    );

                    $operation = M('file')->add($insert_data);


                    $upload_ret['url_size'] =  '/Uploads/'.$type.'/' . $upload_ret['savepath'] . $width.'-' . $upload_ret['savename'];
                    $upload_ret['domain_url_size'] =  C('site_url').'/Uploads/'.$type.'/' . $upload_ret['savepath'] . $width.'-' . $upload_ret['savename'];

                    $upload_ret['url'] = $imgUrl;
                    $upload_ret['domain_url'] = C('site_url').$imgUrl;

                    break;
                case 'doc':
                    $upload->maxSize = 524288000;// 设置附件上传大小
                    $upload->exts = array('doc','ppt','docx','pptx','zip');// 设置附件上传类型
                    $upload->rootPath = './Uploads/'.$type.'/'; // 设置附件上传根目录
                    $upload->savePath = ''; // 设置附件上传（子）目录
                    $upload->saveRule = 'time';
                    $upload_ret = $upload->uploadOne($_FILES['file']);
                    if ($upload_ret == false) return showErrorMsg($upload->getError(),100002);


                    $imgUrl = empty($upload_ret) ? '' : '/Uploads/' .$type.'/'. $upload_ret['savepath'] . $upload_ret['savename'];

                    $insert_data = array(
                        'uid' => decode_token(),
                        'name' => $_FILES['file']['name'],
                        'path' => $imgUrl,
                        'ext' => $_FILES['file']['type'],
                        'size' => $_FILES['file']['size'],
                        'md5' => $upload_ret['md5'],
                        'sha1' => $upload_ret['sha1'],
                        'location' => 'location',
                        'download' => '0',
                        'url' => C('site_url') . $imgUrl,
                        'thumb' => '/Uploads/photo/' . $upload_ret['savepath'] . '500-' . $upload_ret['savename'],
                        'create_time' => NOW_TIME,
                        'update_time' => NOW_TIME,
                    );
                    $operation = M('file')->add($insert_data);

                    $upload_ret['url'] = $imgUrl;
                    $upload_ret['domain_url'] = C('site_url').$imgUrl;

                    break;
            }

            $data = array(
                'code' => 0,
                'msg' => '上传成功',
                'data' => $upload_ret
            );

//            $this->success($upload_ret);
            echo  json_encode($data);
            exit();
//            return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$upload_ret);
        }
    }
}


?>